#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

char *my="hello=1234";

int main(int argc,char *argv[]) {
    pid_t pid;
    extern char** environ;
    // 创建子进程
    pid = fork();
    
    if (pid < 0) {
        // fork失败
        perror("fork failed");
        exit(EXIT_FAILURE);
    } else if (pid == 0) {
        
        char*const myargv[]={"mycmd","1","2",NULL};
        //char*const myenv[]={"hello=1234","word=1",NULL};
        putenv(my);
        execvpe("./mycmd",myargv,environ);
        //execl("./mycmd","mycmd",NULL);
        
        
        // 子进程
        
        // 使用execl替换当前进程为ls命令
        // 第一个参数是命令路径，第二个是命令名，后面是参数列表，最后以NULL结束
        //execl("/bin/ls", "ls", "-l", NULL);
        //char *const myargv[]={
        //    (char*)"ls",
        //    (char*)"-l",
        //    (char*)"-a",
        //    (char*)"-i",
        //    (char*)"-n",
        //    NULL
        //};

        
        //char *const myargv[]={
        //    (char*)"ls",
        //    (char*)"-a",
        //    (char*)"-l",
        //    (char*)"-n",
        //    NULL
        //};

        //execvp(myargv[0],myargv);

        //char **myargv=&argv[1];
            //char *const myargv[]={(char*)"pwd",NULL};
        //execv(myargv[0],myargv);
        //execlp("ls","ls","-l","-a",NULL);
        
        // 如果execl返回，说明调用失败
        perror("execl failed");
        exit(EXIT_FAILURE);
    } else {
        // 父进程
        
        // 等待子进程结束
        int status;
        waitpid(pid, &status, 0);
        
        }
    
    return EXIT_SUCCESS;
}
